﻿// ***********************************************************************
// Assembly         : YERIL_PI_INT
// Author           : Diego Bonilla
// Created          : 03-02-2014
//
// Last Modified By : Diego Bonilla
// Last Modified On : 03-02-2014
// ***********************************************************************
// <copyright file="MaterialProveedor.aspx.cs" company="Development Solutions CR">
//     Copyright (c) Development Solutions CR. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// The Login namespace.
/// </summary>
namespace YERIL_PI_INT.PaginasWebs.Modulos.Login
{
    /// <summary>
    /// Class MaterialProveedor.
    /// </summary>
    public partial class MaterialProveedor : System.Web.UI.Page
    {

        /// <summary>
        /// The mensaje error
        /// </summary>
        public string MensajeError = "";
        /// <summary>
        /// The codigo material
        /// </summary>
        static int codigoMaterial = 0;
        /// <summary>
        /// The seleccion
        /// </summary>
        public string seleccion = "";
        /// <summary>
        /// The codigo_pro
        /// </summary>
        static int codigo_pro = 0;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Codigo"] != null)
            {
                codigoMaterial = Convert.ToInt32(Session["Codigo"]);

            } 
            
            CargarGrid();

            if (!IsPostBack)
            {
               
                Cargar_Lista();
 
            }
        }

        /// <summary>
        /// Cargar_s the lista.
        /// </summary>
        private void Cargar_Lista()
        {
            try
            {
                WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                List<WSGestion.MOD_Proveedores> ListaEstadoProveedores = wsgest.SEL_GRID_PROVEEDORES().ToList();
                this.lbxProveedores.DataSource = ListaEstadoProveedores;
                this.lbxProveedores.DataValueField = "PRO_codproveedor";
                this.lbxProveedores.DataTextField = "PRO_nombre";
                this.lbxProveedores.DataBind();

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }
        /// <summary>
        /// Cargars the grid.
        /// </summary>
        public void CargarGrid()
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Código Proveedor", Type.GetType("System.String"));
                dt.Columns.Add("Nombre Proveedor", Type.GetType("System.String"));

                int cont = dt.Columns.Count;
                for (int i = 0; i < listaProveedores().Count; i++)
                {
                    dt.Rows.Add();
                    dt.Rows[dt.Rows.Count - 1]["Código Proveedor"] = listaProveedores()[i].PRO_codproveedor;
                    dt.Rows[dt.Rows.Count - 1]["Nombre Proveedor"] = listaProveedores()[i].PRO_nombre;

                }

                gvMaterialProveedor.DataSource = dt;
                gvMaterialProveedor.DataBind();
            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }

            


        }

        /// <summary>
        /// Listas the proveedores.
        /// </summary>
        /// <returns>List{WSGestion.MOD_Proveedores}.</returns>
        private List<WSGestion.MOD_Proveedores> listaProveedores()
        {

            WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
            wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
            List<WSGestion.MOD_Proveedores> ListaProveedores = wsgest.SEL_Grid_Material_Proveedor(codigoMaterial).ToList();
            return ListaProveedores;

        }
        /// <summary>
        /// Handles the Click event of the btnAgregar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnAgregar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
               
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    int codProveedor =Convert.ToInt32(lbxProveedores.SelectedValue);
                    int cant_pro = wsgest.SEL_MaterialProveedorByIDs(codigoMaterial, codProveedor);
                    if (cant_pro == 0)
                    {
                        if (wsgest.CRE_Provedor_Material(codProveedor, codigoMaterial))
                        {
                            MensajeError = "Proveedor " + lbxProveedores.SelectedItem + " se ha agregado.";
                            CargarGrid();
                        }
                        else
                        {
                            MensajeError = "Procedimiento Almacenado no aceptado";
                        }

                    }

                    else
                    {
                        MensajeError = "No es valido introducir el mismo proveedor. Por favor, intente con otro de la lista.";
                    }
                    
                   

                

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnEliminar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnEliminar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (codigo_pro == 0)
                {
                    MensajeError = "No se ha seleccionado un Proveedor";
                }
                else
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    wsgest.DEL_ProveedorMaterial(codigo_pro, codigoMaterial);
                    CargarGrid();
                }
            }
            catch (Exception exc)
            {

                MensajeError = exc.Message.ToString();
            }

        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the gvMaterialProveedor control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void gvMaterialProveedor_SelectedIndexChanged(object sender, EventArgs e)
        {
            seleccion = gvMaterialProveedor.SelectedRow.Cells[2].Text;
            codigo_pro = Convert.ToInt32(gvMaterialProveedor.SelectedRow.Cells[1].Text);
            MensajeError = "Se ha seleccionado el cliente: " + seleccion;
        }

        /// <summary>
        /// Handles the Click event of the btnAgregarCliente control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnAgregarCliente_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("GridMateriales.aspx");
        }
    }
}